<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对数据库执行一次查询</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.prepare.html">mysqli::prepare</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.real-connect.html">mysqli::real_connect</a></div>
 <div class="up"><a href="class.mysqli.html">MySQLi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mysqli.query" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mysqli::query</h1>
  <h1 class="refname">mysqli_query</h1>
  <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">mysqli::query</span> -- <span class="refname">mysqli_query</span> &mdash; <span class="dc-title">对数据库执行一次查询</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mysqli.query-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>mysqli::query</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$resultmode</code><span class="initializer"> = MYSQLI_STORE_RESULT</span></span>
  ] )</div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>mysqli_query</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.mysqli.html" class="type mysqli">mysqli</a></span> <code class="parameter">$link</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$resultmode</code><span class="initializer"> = MYSQLI_STORE_RESULT</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   Performs a <em><code class="parameter">query</code></em> against the database.
  </p>
  <p class="para">
   For non-DML queries (not INSERT, UPDATE or DELETE),
   this function is similar to calling 
   <span class="function"><a href="mysqli.real-query.html" class="function">mysqli_real_query()</a></span> followed by either
   <span class="function"><a href="mysqli.use-result.html" class="function">mysqli_use_result()</a></span> or 
   <span class="function"><a href="mysqli.store-result.html" class="function">mysqli_store_result()</a></span>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    In the case where you pass a statement to
    <span class="function"><strong>mysqli_query()</strong></span> that is longer than
    <em>max_allowed_packet</em> of the server, the returned
    error codes are different depending on whether you are using MySQL
    Native Driver (<em>mysqlnd</em>) or MySQL Client Library
    (<em>libmysqlclient</em>). The behavior is as follows:
   </p>
   <ul class="itemizedlist">
    <li class="listitem">
     <p class="para">
      <em>mysqlnd</em> on Linux returns an error code of 1153.
      The error message means "<span class="quote">got a packet bigger than
      <em>max_allowed_packet</em> bytes</span>".
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <em>mysqlnd</em> on Windows returns an error code 2006.
      This error message means "<span class="quote">server has gone away</span>".
     </p>
    </li>
    <li class="listitem">
     <p class="para">
      <em>libmysqlclient</em> on all platforms returns an error code
      2006. This error message means "<span class="quote">server has gone
      away</span>".
     </p>
    </li>
   </ul>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-mysqli.query-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
<em><code class="parameter">
link</code></em></dt>
<dd>
<p class="para">仅以过程化样式：由<span class="function"><a href="function.mysqli-connect.html" class="function">mysqli_connect()</a></span> 或 <span class="function"><a href="mysqli.init.html" class="function">mysqli_init()</a></span>
返回的链接标识。</p></dd>

    
     <dt>
<em><code class="parameter">query</code></em></dt>

     <dd>

      <p class="para">
       The query string.
      </p>
      <p class="para">
       Data inside the query should be <a href="mysqli.real-escape-string.html" class="link">properly escaped</a>.
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">resultmode</code></em></dt>

     <dd>

      <p class="para">
       Either the constant <strong><code>MYSQLI_USE_RESULT</code></strong> or
       <strong><code>MYSQLI_STORE_RESULT</code></strong> depending on the desired
       behavior. By default, <strong><code>MYSQLI_STORE_RESULT</code></strong> is used.
      </p>
      <p class="para">
       If you use <strong><code>MYSQLI_USE_RESULT</code></strong> all subsequent calls
       will return error <em>Commands out of sync</em> unless you
       call <span class="function"><a href="mysqli-result.free.html" class="function">mysqli_free_result()</a></span>
      </p>
      <p class="para">
       With <strong><code>MYSQLI_ASYNC</code></strong> (available with mysqlnd), it is
       possible to perform query asynchronously.
       <span class="function"><a href="mysqli.poll.html" class="function">mysqli_poll()</a></span> is then used to get results from such
       queries.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mysqli.query-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   失败时返回 <strong><code>FALSE</code></strong>，通过<span class="function"><strong>mysqli_query()</strong></span> 成功执行<em>SELECT, SHOW, DESCRIBE</em>或
   <em>EXPLAIN</em>查询会返回一个<a href="class.mysqli-result.html" class="classname">mysqli_result</a> 对象，其他查询则返回<strong><code>TRUE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-mysqli.query-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        Added the ability of async queries.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mysqli.query-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1648">
   <p><strong>Example #1 <span class="methodname"><strong>mysqli::query()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$mysqli&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;check&nbsp;connection&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_errno</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Connect&nbsp;failed:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">connect_error</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;exit();<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;table&nbsp;doesn't&nbsp;return&nbsp;a&nbsp;resultset&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE&nbsp;TEMPORARY&nbsp;TABLE&nbsp;myCity&nbsp;LIKE&nbsp;City"</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Table&nbsp;myCity&nbsp;successfully&nbsp;created.\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Select&nbsp;queries&nbsp;return&nbsp;a&nbsp;resultset&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;Name&nbsp;FROM&nbsp;City&nbsp;LIMIT&nbsp;10"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Select&nbsp;returned&nbsp;%d&nbsp;rows.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">num_rows</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;free&nbsp;result&nbsp;set&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;If&nbsp;we&nbsp;have&nbsp;to&nbsp;retrieve&nbsp;large&nbsp;amount&nbsp;of&nbsp;data&nbsp;we&nbsp;use&nbsp;MYSQLI_USE_RESULT&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;City"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MYSQLI_USE_RESULT</span><span style="color: #007700">))&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Note,&nbsp;that&nbsp;we&nbsp;can't&nbsp;execute&nbsp;any&nbsp;functions&nbsp;which&nbsp;interact&nbsp;with&nbsp;the<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;until&nbsp;result&nbsp;set&nbsp;was&nbsp;closed.&nbsp;All&nbsp;calls&nbsp;will&nbsp;return&nbsp;an<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'out&nbsp;of&nbsp;sync'&nbsp;error&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SET&nbsp;@a:='this&nbsp;will&nbsp;not&nbsp;work'"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Error:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">error</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$link&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;check&nbsp;connection&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Connect&nbsp;failed:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;exit();<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Create&nbsp;table&nbsp;doesn't&nbsp;return&nbsp;a&nbsp;resultset&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"CREATE&nbsp;TEMPORARY&nbsp;TABLE&nbsp;myCity&nbsp;LIKE&nbsp;City"</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">TRUE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Table&nbsp;myCity&nbsp;successfully&nbsp;created.\n"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Select&nbsp;queries&nbsp;return&nbsp;a&nbsp;resultset&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;Name&nbsp;FROM&nbsp;City&nbsp;LIMIT&nbsp;10"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Select&nbsp;returned&nbsp;%d&nbsp;rows.\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysqli_num_rows</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">));<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;free&nbsp;result&nbsp;set&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysqli_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;If&nbsp;we&nbsp;have&nbsp;to&nbsp;retrieve&nbsp;large&nbsp;amount&nbsp;of&nbsp;data&nbsp;we&nbsp;use&nbsp;MYSQLI_USE_RESULT&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;*&nbsp;FROM&nbsp;City"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">MYSQLI_USE_RESULT</span><span style="color: #007700">))&nbsp;{<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Note,&nbsp;that&nbsp;we&nbsp;can't&nbsp;execute&nbsp;any&nbsp;functions&nbsp;which&nbsp;interact&nbsp;with&nbsp;the<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;server&nbsp;until&nbsp;result&nbsp;set&nbsp;was&nbsp;closed.&nbsp;All&nbsp;calls&nbsp;will&nbsp;return&nbsp;an<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'out&nbsp;of&nbsp;sync'&nbsp;error&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(!</span><span style="color: #0000BB">mysqli_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SET&nbsp;@a:='this&nbsp;will&nbsp;not&nbsp;work'"</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Error:&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">mysqli_error</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">mysqli_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">mysqli_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Table myCity successfully created.
Select returned 10 rows.
Error: Commands out of sync;  You can&#039;t run this command now
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-mysqli.query-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="mysqli.real-query.html" class="function" rel="rdfs-seeAlso">mysqli_real_query()</a> - 执行一个mysql查询</span></li>
    <li class="member"><span class="function"><a href="mysqli.multi-query.html" class="function" rel="rdfs-seeAlso">mysqli_multi_query()</a> - Performs a query on the database</span></li>
    <li class="member"><span class="function"><a href="mysqli-result.free.html" class="function" rel="rdfs-seeAlso">mysqli_free_result()</a> - Frees the memory associated with a result</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mysqli.prepare.html">mysqli::prepare</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mysqli.real-connect.html">mysqli::real_connect</a></div>
 <div class="up"><a href="class.mysqli.html">MySQLi</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
